import java.awt.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class HeadObj extends GameutilsOBJ{
    //方向 up down left right
    private String direction ="right";//默认反向向右

    public String getDirection() {
        return direction;
    }

    public void setDirection(String direction) {
        this.direction = direction;
    }

    public HeadObj(Image img, int x, int y, GameWin frame) {
        super(img, x, y, frame);
        this.frame.addKeyListener(new KeyAdapter() {
            @Override
            public void keyPressed(KeyEvent e) {
                changeDirection(e);
            }
        });
    }

    public void changeDirection(KeyEvent e){
        switch (e.getKeyCode()){
            case KeyEvent.VK_A:
                if (!"right".equals(direction)){
                    direction ="left";
                    img = GameUtils.leftImg;
                }
                break;
            case KeyEvent.VK_D:
                if (!"left".equals(direction)){
                    direction ="right";
                    img = GameUtils.rightImg;
                }
                break;
            case KeyEvent.VK_W:
                if (!"down".equals(direction)){
                    direction ="up";
                    img = GameUtils.upImg;
                }
                break;
            case KeyEvent.VK_S:
                if (!"up".equals(direction)){
                    direction ="down";
                    img = GameUtils.downImg;
                }
                break;
            default :
                break;
        }
    }

    public void move() {
        //蛇身体的移动
        java.util.List<BodyObj>bodyObjList = this.frame.bodyObjList;
        //空列表检查
        if (bodyObjList.isEmpty()) {
            // 如果没有蛇身，直接移动蛇头
            switch (direction){
                case "up":
                    y -=height;
                    break;
                case "down":
                    y +=height;
                    break;
                case "left":
                    x -=width;
                    break;
                case "right":
                    x +=width;
                    break;
                default:
                    break;
            }
            return;
        }
        for (int i = bodyObjList.size() - 1; i >= 1 ; i--) {
            bodyObjList.get(i).x = bodyObjList.get(i-1).x;
            bodyObjList.get(i).y = bodyObjList.get(i-1).y;
            //蛇头与身体的碰撞判断
            if (this.x ==  bodyObjList.get(i).x && this.y ==  bodyObjList.get(i).y){
                //失败
                GameWin.state = 3;
            }
        }
        bodyObjList.get(0).x = this.x;
        bodyObjList.get(0).y = this.y;
        //蛇头的移动
        switch (direction){
            case "up":
                y -=height;
                break;
            case "down":
                y +=height;
                break;
            case "left":
                x -=width;
                break;
            case "right":
                x +=width;
                break;
            default:
                break;
        }
    }

    private int getCurrentLevelTarget() {
        if (GameUtils.level <= GameUtils.LEVEL_TARGETS.length) {
            return GameUtils.LEVEL_TARGETS[GameUtils.level - 1];
        }
        return 15;
    }

    @Override
    public void paintSelf(Graphics g) {
        super.paintSelf(g);

        //身体最后一节的坐标
        Integer newX = null;
        Integer newY = null;
        boolean ateFood = false;

        // 检查是否吃到任何食物
        for (int i = 0; i < frame.foodList.size(); i++) {
            FoodObj currentFood = frame.foodList.get(i);
            if (currentFood != null && this.x == currentFood.x && this.y == currentFood.y) {
                // 吃到食物
                if (GameUtils.level == 4) {
                    // ========== 修复开始：第四关特殊处理 ==========
                    // 移除被吃的食物
                    frame.foodList.remove(i);
                    // 生成新食物并确保frame正确设置
                    FoodObj newFood = new FoodObj().getFoodForLevel4(frame);
                    frame.foodList.add(newFood);
                    // ========== 修复结束 ==========
                } else {
                    // 其他关卡：重新生成单个食物
                    FoodObj foodGenerator = new FoodObj();
                    foodGenerator.frame = this.frame;
                    frame.foodList.set(0, foodGenerator.getFood());
                }

                //获取蛇身的最后一个元素
                BodyObj lastBody = this.frame.bodyObjList.get(this.frame.bodyObjList.size() - 1);
                newX = lastBody.x;
                newY = lastBody.y;
                //分数+1
                this.frame.score++;
                ateFood = true;
                break;
            }
        }

        //通关判断
        int currentTarget = getCurrentLevelTarget();
        if (this.frame.score >= currentTarget && GameUtils.level != 5){
            //通关
            GameWin.state = 4;
        }

        move();

        if (ateFood && newX != null && newY != null){
            this.frame.bodyObjList.add(new BodyObj(GameUtils.bodyImg,newX,newY,this.frame));
        }

        //越界处理
        if (x < 0){
            x = 570;
        }else if (x > 570){
            x = 0;
        } else if (y < 30) {
            y = 570;
        } else if (y > 570) {
            y = 30;
        }
    }
}